package com.kevms.phonewars;

import org.json.JSONArray;
import org.json.JSONObject;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

/** 
* @class JoinMatchEntry
* 
* @brief Activity used to join a match. (or delete it)
* 
* This Activity displays match information, along with
* buttons for joining the match. If you have permission,
* there is also a button for deleting the match.
*/ 
public class JoinMatchEntry extends Activity {
	
	private Context mContext;
	private String mMatchID;
	private int rank;
	private boolean owner;
	
	private SQLiteDatabase db;
	private MySQLiteHelper dbHelper;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.join_match_entry);
		mContext = this;
		
		dbHelper = MySQLiteHelper.getInstance(getApplicationContext());
		db = dbHelper.getWritableDatabase();
		LinearLayout l = (LinearLayout)findViewById(R.id.topLayout);
    	l.setBackgroundColor(dbHelper.getBgColor(db));
		
		mMatchID = getIntent().getStringExtra("match_id");
		
		TextView tvMatchName = (TextView)findViewById(R.id.tvMatchName);
		TextView tvMatchType = (TextView)findViewById(R.id.tvMatchType);
		TextView tvPlayerCount = (TextView)findViewById(R.id.tvPlayerCount);
		TextView tvHost = (TextView)findViewById(R.id.tvHost);
		TextView tvGunRadius = (TextView)findViewById(R.id.tvGunRadius);
		TextView tvGunRange = (TextView)findViewById(R.id.tvGunRange);
		
		WebService webService = WebService.getInstance(mContext);
		try {
			JSONObject data = webService.getMatch(mMatchID);
			
			tvMatchName.setText(data.getString("name"));
			tvMatchType.setText(data.getString("type"));
			tvHost.setText(data.getString("host"));
			tvPlayerCount.setText(data.getString("count"));

			tvGunRadius.setText(""+data.getInt("gun_radius"));
			tvGunRange.setText(""+data.getInt("gun_range"));
			rank = data.getInt("rank");
			owner = data.getBoolean("owner");
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		Button btnJoinMatch = (Button)findViewById(R.id.btnJoinMatch);
		
		btnJoinMatch.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				PWApplication appState = ((PWApplication)getApplicationContext());
				WebService webService = WebService.getInstance(mContext);
				Player p;
				
				appState.clearPlayerList();
				
				try {
					JSONArray jArray = webService.joinMatch(mMatchID);
					
					JSONObject jObject;
					for(int i = 0; i < jArray.length(); i++) {
						jObject = jArray.getJSONObject(i);
						p = new Player();
						p.setId(jObject.getInt("id"));
						p.setName(jObject.getString("name"));
						p.setKills(jObject.getInt("kills"));
						p.setDeaths(jObject.getInt("deaths"));
						appState.addPlayer(p);
					}
					
					Intent intent = new Intent(mContext, Combat.class);
					startActivityForResult(intent,0);
				} catch (Exception e) {
					showAlert(e.getMessage());
				}
			}
		});
		
		Button btnDeleteMatch = (Button)findViewById(R.id.btnDeleteMatch);
		if(rank > 1 || owner) {
			btnDeleteMatch.setVisibility(View.VISIBLE);
		}
		
		btnDeleteMatch.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				deleteConfirm();
			}
		});
	}
	
	private void deleteConfirm() {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setMessage("Delete match?")
			.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int id) {
					WebService webService = WebService.getInstance(mContext);
					try {
						webService.deleteMatch(mMatchID);
						finish();
					} catch (Exception e) {
						showAlert(e.getMessage());
					}
				}
			})
			.setNegativeButton("No", new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int id) {
					dialog.cancel();
				}
			});
		AlertDialog alert = builder.create();
		alert.show();
	}
	
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		finish();
    }
	
	private void showAlert(String message) {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setMessage(message)
			.setCancelable(false)
			.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int id) {
					dialog.cancel();
				}
			});
		AlertDialog alert = builder.create();
		alert.show();
	}
}
